import java.net.URL;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.StackPaneBuilder;
import javafx.scene.paint.Paint;
import javafx.scene.text.Text;
import javafx.scene.text.TextBuilder;
import javafx.stage.Stage;

public class HelloJax extends Application {
 
 String lastAction = null;

 @Override
 public void start(Stage stage) throws Exception {

  stage.setTitle("Das erste Beipiel im Workshop");
  URL url = getClass().getResource("/calculator.fxml");
  Node calculator = FXMLLoader.load(url);

  StackPane pane = StackPaneBuilder.create().children(calculator).build();
  Scene scene = SceneBuilder.create().root(pane).build();
  stage.setScene(scene);
  final Text display = (Text) scene.lookup("#display");

  for (int i = 0; i <= 9; i++) {
   final Button btn = (Button) scene.lookup("#btn" + i);
   if (btn != null) {
    btn.setOnAction(new EventHandler<ActionEvent>() {

     @Override
     public void handle(ActionEvent arg0) {
      
      int d = Integer.parseInt(display.getText());
      if(lastAction != null) {
       d = 0;
      }
      
      int b = Integer.parseInt(btn.getText());
      display.setText(String.valueOf(d * 10 + b));
     }
    });
   }
  }

  Button btnClear = (Button) scene.lookup("#btnClear");

  btnClear.setOnAction(new EventHandler<ActionEvent>() {

   @Override
   public void handle(ActionEvent arg0) {
    display.setText("0");
   }
  });

  stage.show();
 }

 public static void main(String[] args) {
  launch(args);
 }

}
